<script setup lang="ts">


import myAxios from "../plugins/myAxios.ts";
import {showFailToast, showSuccessToast} from "vant";
import {useRoute, useRouter} from "vue-router";
import {ref} from "vue";

const userAccount = ref('');
const userPassword = ref('');

const router = useRouter();
const route = useRoute();

const onSubmit = async () => {
  const res = await myAxios.post('/user/login',{
    userAccount:userAccount.value,
    userPassword:userPassword.value,
  })
  console.log(res, '登录请求');
  if (res.code === 0 && res.data) {
    showSuccessToast("登录成功");

    // router.replace('/');
    // 跳转到之前的页面    ??是一个简写，如果是true，则使用前面的，否则使用后面的
    const redirectUrl = route.query?.redirect as string ?? "/";
    window.location.href = redirectUrl;


  } else {
    showFailToast("登录失败");
  }

};

</script>

<template>
  <van-form @submit="onSubmit">
    <van-cell-group inset>
      <van-field
          v-model="userAccount"
          name="userAccount"
          label="账户"
          placeholder="账户"
          :rules="[{ required: true, message: '请填写账户' }]"
      />
      <van-field
          v-model="userPassword"
          type="password"
          name="userPassword"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
      />
    </van-cell-group>
    <div style="margin: 16px;">
      <van-button round block type="primary" native-type="submit">
        提交
      </van-button>
    </div>
  </van-form>
</template>

<style scoped>

</style>